package mainService.services;

import java.util.ArrayList;

import mainService.HTTPService;
import mainService.ServiceParams;
import mainService.data.IpClient;
import mainService.data.UsersConnected;

/**
 * Classe che implementa il servizio di completamento di un task.
 * 
 * @author Mattia Grecchi, Daniel Procop
 * 
 */
public class CompletedTaskService extends WriterServices implements HTTPService {
	@Override
	public ArrayList<String> getServiceResult(ServiceParams params) {

		if (!UsersConnected.getUsersConnected()
				.getUtente(IpClient.getSingleton().getIp()).getRuolo()
				.equalsIgnoreCase("Project Manager")) {
			params.addParam("file", "noPermessi.html");
			return (new HttpFileServer()).getServiceResult(params);
		}
		String id_task = params.getValue("id_task");

		int id_project = UsersConnected.getUsersConnected()
				.getUtente(IpClient.getSingleton().getIp()).getId_project();
		
		String cmd = "UPDATE tasks SET isCompleted = (1-isCompleted) WHERE id_task="
				+ id_task + " AND id_project=" + id_project + ";";
		boolean var = updateDb(cmd);
		if (!var) {
			params.addParam("file", "modificaTaskFallita.html.html");
		} else {
			params.addParam("file", "modificaTaskRiuscita.html");
		}

		return (new HttpFileServer()).getServiceResult(params);
	}
}
